package com.shop.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.bean.Admin;
import com.shop.service.AdminService;
import com.shop.service.impl.AdminServiceImpl;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String account=request.getParameter("username");
		String password=request.getParameter("password");
		Admin admin=new Admin();
		admin.setAccount(account);
		admin.setPassword(password);
		AdminService as=new AdminServiceImpl();
		Map<String,Object> result=as.login(admin);
		if(result.get("loginAdmin")!=null) {//登录成功，保存状态
			HttpSession session=request.getSession();
			session.setAttribute("loginAdmin",result.get("loginAdmin"));
			response.sendRedirect("GoodsServlet");
		}else {
			request.setAttribute("msg",result.get("msg"));
			request.getRequestDispatcher("index.jsp").forward(request, response);;
		}
	}
}
